# -*- coding:utf-8 -*-
__author__ = 'xigu007'
__all__ = ('ShapeBackground', 'BorderImageBackground')
from kivy.clock import Clock
from kivy.graphics import Rectangle, Color, BorderImage,InstructionGroup
from kivy.metrics import sp
import os
from base import Group
from content import ShapeProperty

class VtInstructGroup(Group):
    def __init__(self, **kwargs):
        self._vt_instruction = None
        self._color_instruction = None
        #是否已经添加进compositewidget的canvas
        super(VtInstructGroup, self).__init__(**kwargs)

    def add_instruction(self):
        instruction_group = self._instruction_group
        added_flg = self.push_state(instruction_group)
        if self._vt_instruction is None:
            self.build_instruction()
        if not added_flg:
            if self._vt_instruction:
                instruction_group.clear()
                if self._clear_color_state:
                    self._color_instruction = Color(*self._color)
                    instruction_group.add(self._color_instruction)
                instruction_group.add(self._vt_instruction)
                instruction_group.add(Color(1,1,1,1))
                for content in self.contents_children:
                    instruction_group.add(content._instruction_group)
                self._added_flg = True
        for content in self.contents_children:
            content.add_instruction()
        self.pop_state(instruction_group)

class BorderImageBackground(VtInstructGroup):
    def __init__(self,**kwargs):
        self._source = ''
        self._border = (10,10,10,10)
        super(BorderImageBackground,self).__init__(**kwargs)
        self._vt_instruction = BorderImage(pos = (0,0),size=(0,0))

    def do_apply(self):
        if self._vt_instruction:
            self._vt_instruction.size = self.group_content_size
            self._vt_instruction.pos = self.group_content_pos
        super(BorderImageBackground, self).do_apply()

    @property
    def image_source(self):
        return self._source

    @image_source.setter
    def image_source(self, value):
        if value != self._source:
            self._source = value
            if self._vt_instruction:
                self._vt_instruction.source = self._source

    @property
    def border(self):
        return self._border

    @border.setter
    def border(self, value):
        self._border = value
        if self._vt_instruction:
            self._vt_instruction.border = self._border

class ShapeBackground(VtInstructGroup, ShapeProperty):

    def do_apply(self):
        if self._vt_instruction:
            self.change_property()
        super(ShapeBackground,self).do_apply()

    def get_content_real_size(self):
        return max(5, self.group_content_size[0]), max(5, self.group_content_size[1])

    def get_content_real_pos(self):
        return self.group_content_pos

    @property
    def color(self):
        return self._color

    @color.setter
    def color(self, value):
        self._color = value
        self._clear_color_state = True
        if self._color_instruction:
            self._color_instruction.rgba = self._color
